const superagent = require("superagent")
var charset = require('superagent-charset');
const cheerio = require('cheerio');
const axios = require('axios')
charset(superagent); //设置字符

var BASE_URL='https://guahao.zjol.com.cn/doctor/0571057102/64343';
var LAST='';
var timer='';
// callbacks
function getYuYuelink(count){
    if(count>1) clearTimeout(timer);
    if(count==3000000) {
        console.log('查询结束')
        return;
    }
    superagent
    .get(BASE_URL)
    .charset('utf-8')
    .buffer(true)
    .end((error, response) => {
        //console.log(response.text)
        //结合 cheerio
        var dom = (response||{}).text;
        if(dom){
            const $ = cheerio.load(dom,{ decodeEntities: false });
            var s1 = $(".doc-detail-content-mor").text().trim().replace(/\s+/g,"");
            var s2 = $(".doc-detail-content-noon").text().trim().replace(/\s+/g,"");
            var resStr= s1+'\n'+s2;
            console.log(new Date().getTime()+ ' : '+resStr.replace(/\s+/g,""))
            if(resStr!=LAST){
                LAST=resStr;
                console.log(LAST)
                axios.get('http://wxmsg.dingliqc.com/demo/send/custom/UID_LET5BYBwfyMHAOizxrH4gkkhAvhv?content='+encodeURIComponent(LAST))
                .then((response) => {
                    console.log('success')
                })
                .catch((error) => {
                    console.log('error')
                });
            }
        }
        count++;
        timer=setTimeout(() => {
            getYuYuelink(count);
        }, 5000);
       
    })
}

getYuYuelink(1);
